var gulp = require('gulp');
var shell = require('gulp-shell');

// [Neo] TODO change here
var project_name = 'edu';
// [Neo] refer to `settings.gradle`
var module_name = 'edu';
// [Neo] static file from local
var static_web_path = '-';
// [Neo] static file to http server
var static_web_name = '-';

var docker_web_name = project_name + '_web_1';
var docker_db_name = project_name + '_db_1';
var docker_cache_name = project_name + '_cache_1';

var docker_project_volume = '/srv/docker/' + project_name;
var docker_web_path = docker_project_volume + '/web';
var docker_db_path = docker_project_volume + '/db';
var docker_cache_path = docker_project_volume + '/cache';

var security_java_file = '/etc/java-8-openjdk/security/java.security';

var nginx_server_conf_path = '/etc/nginx/default.d';
var nginx_reload_cmd = 'sudo systemctl reload nginx';
var nginx_server_conf_path_mac = '/usr/local/etc/nginx/default.d';
var nginx_reload_cmd_mac = 'sudo brew services restart nginx';
var nginx_root = '/srv/http';
var nginx_html_path = '/usr/share/nginx/html';
var nginx_html_path_mac = '/usr/local/var/www';

var uploaded_path = docker_project_volume + '/web';
var backup_path = '/srv/backup/' + project_name;

gulp.task('default', shell.task([
  'npm run dev'
]));

gulp.task('build', shell.task([
  'npm run build',
  '7za a -r kcgl-web.zip ./dist/*',
]));

gulp.task('zip', shell.task([
  '7za a -r kcgl-web.zip ./dist/*',
]));

gulp.task('scp', shell.task([
  'scp ./kcgl-web.zip zyh@www.jnaw.top:',
]));

gulp.task('zyh', shell.task([
  'npm run build',
  '7za a -r kcgl-web.zip ./dist/*',
  'scp ./kcgl-web.zip zyh@www.jnaw.top:',
]));

gulp.task('deploy', shell.task([
  'npm run build',
  'sudo cp -rf ./dist/* /srv/http/hjl-web/',
  'find . -type d -exec chmod +755 {} \\'
]));

gulp.task('backup', shell.task([
  'if [ ! -e ' + nginx_root
  + ' ]; then if [ "Linux" = $(uname) ]; then sudo ln -s ' + nginx_html_path
  + ' ' + nginx_root + '; else sudo mkdir -p /srv; sudo ln -s '
  + nginx_html_path_mac + ' ' + nginx_root + '; fi; else if [ ! -L '
  + nginx_root + ' ]; then echo ' + nginx_root
  + ' check failed!; exit 1; fi; fi',
  'if [ "Linux" = $(uname) ]; then sudo mkdir -p ' + docker_project_volume
  + '/{web,db,cache} && sudo chown -R $USER ' + docker_project_volume + '; fi',

  'if [ "Linux" = $(uname) ]; then sudo mkdir -p ' + backup_path
  + '/{sql,uploaded} && sudo chown -R $USER ' + backup_path + '; fi',

  'if [ "Linux" = $(uname) ]; then sudo mkdir -p ' + uploaded_path
  + ' && sudo chown -R $USER ' + uploaded_path + '; fi',

  'if [ "Linux" = $(uname) ]; then sudo ln -s ' + backup_path + '/uploaded'
  + ' ' + uploaded_path + '; fi;',

]));




